package com.snail.billing.data;

import java.util.UUID;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.android.os.MyEngine;
import com.snail.billing.util.AesUtil;
import com.snail.billing.util.BillingConfiguration;

public class AccountManager {

	private static final byte SAVE_MAX = 6;

	public static void checkUUID() {
		// UUID处理
		BillingConfiguration billingConfiguration = new BillingConfiguration(MyEngine.getEngine().getApplicationContext());
		AccountFile accountFile = new AccountFile();
		String uuid = billingConfiguration.getUUID();
		if (TextUtils.isEmpty(uuid)) {
			uuid = accountFile.getUUID();
			if (TextUtils.isEmpty(uuid)) {
				uuid = createUUID();
				accountFile.setUUID(uuid);
			}
			billingConfiguration.setUUID(uuid);
		} else {
			accountFile.setUUID(uuid);
		}
	}

	private static String createUUID() {
		TelephonyManager telephonyManager = (TelephonyManager) MyEngine.getEngine().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
		String simSerial = telephonyManager.getSimSerialNumber();
		if (!TextUtils.isEmpty(simSerial)) {
			if (simSerial.length() > 10) {
				return simSerial;
			} else if (simSerial.length() > 3) {
				return simSerial + telephonyManager.getDeviceId();
			}
		}
		String phoneNumber = telephonyManager.getLine1Number();
		if (!TextUtils.isEmpty(phoneNumber) && phoneNumber.length() >= 5) {
			return phoneNumber;
		}
		return UUID.randomUUID().toString();
	}

	public static String getUUID() {
		BillingConfiguration billingConfiguration = new BillingConfiguration(MyEngine.getEngine().getApplicationContext());
		return billingConfiguration.getUUID();
	}

	public static void checkAccount() {
		// Account处理
		BillingConfiguration billingConfiguration = new BillingConfiguration(MyEngine.getEngine().getApplicationContext());
		AccountFile accountFile = new AccountFile();
		String accountJsonString = billingConfiguration.getAccountJson();
		if (TextUtils.isEmpty(accountJsonString)) {
			accountJsonString = accountFile.getAccountJson();
			if (!TextUtils.isEmpty(accountJsonString)) {
				billingConfiguration.setAccountJson(accountJsonString);
			}
		} else {
			accountFile.setAccountJson(accountJsonString);
		}
	}

	public static boolean isEmpty() {
		return getAccountArray().length() == 0;
	}

	public static AccountArray getAccountArray() {
		try {
			BillingConfiguration billingConfiguration = new BillingConfiguration(MyEngine.getEngine().getApplicationContext());
			String accountJsonString = billingConfiguration.getAccountJson();
			if (!TextUtils.isEmpty(accountJsonString)) {
				return new AccountArray(AesUtil.decrypt(accountJsonString));
			}
		} catch (Exception e) {
		}
		return new AccountArray();
	}

	public static Account getCurrentAccount() {
		AccountArray accountArray = getAccountArray();
		if (accountArray.length() > 0) {
			return accountArray.getAll().get(0);
		}
		return null;
	}

	public static void saveBindAccount(String account, String pwd) {
		Account newAccount = new Account();
		newAccount.setAccount(account);
		newAccount.setPwd(pwd);
		saveBindAccount(newAccount);
	}
	
	public static void saveBindAccount(Account account) {
		AccountArray accountArray = getAccountArray();
		accountArray.removeFirst();
		// 设置账号类型
		account.setType(Account.TYPE_COMMON);
		accountArray.addFirst(account);
		// 保存
		saveAll(accountArray);
	}

	public static void saveRandomAccount(String account, String pwd) {
		Account newAccount = new Account();
		newAccount.setAccount(account);
		newAccount.setPwd(pwd);
		saveRandomAccount(newAccount);
	}

	public static void saveRandomAccount(Account account) {
		AccountArray accountArray = new AccountArray();
		// 设置账号类型
		account.setType(Account.TYPE_RANDOM);
		accountArray.add(account);
		// 保存
		saveAll(accountArray);
	}


	public static void saveAccount(String account, String pwd) {
		Account newAccount = new Account();
		newAccount.setAccount(account);
		newAccount.setPwd(pwd);
		saveAccount(newAccount);
	}

	public static void saveAccount(Account account) {
		AccountArray oldAccountArray = getAccountArray();
		AccountArray newAccountArray = new AccountArray();
		// 相同账号的删除
		Account tempAccount = null;
		for (Account oldAccount : oldAccountArray.getAll()) {
			if (!oldAccount.getAccount().equals(account.getAccount())) {
				newAccountArray.add(oldAccount);
			} else {
				tempAccount = oldAccount;
			}
		}
		// 设置账号类型
		if (tempAccount != null) {
			account.setType(tempAccount.getType());
		} else {
			// 新账号
			account.setType(Account.TYPE_COMMON);
		}
		// 加入账号
		newAccountArray.addFirst(account);
		// 大于最大值的不是快速进入账号的删除
		if (newAccountArray.length() >= SAVE_MAX) {
			for (int i = newAccountArray.length() - 1; i >= 0; i--) {
				Account newAccount = newAccountArray.getAll().get(i);
				if (newAccount.getType().equals(Account.TYPE_COMMON)) {
					newAccountArray.remove(newAccount);
					break;
				}
			}
		}
		// 保存
		saveAll(newAccountArray);
	}

	private static void saveAll(AccountArray accountArray) {
		String accountJsonString = AesUtil.encrypt(accountArray.toString());
		BillingConfiguration billingConfiguration = new BillingConfiguration(MyEngine.getEngine().getApplicationContext());
		billingConfiguration.setAccountJson(accountJsonString);
		AccountFile accountFile = new AccountFile();
		accountFile.setAccountJson(accountJsonString);
	}
}
